#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import random
from string import ascii_letters
from string import digits


class XRandomUtils(object):
    """
    This module contains boilerplate random helpers.
    """

    @staticmethod
    def random_string(p_n):
        """
        Generate a random alphanumeric string of size ``p_n``.
        """
        return "".join(random.choices(ascii_letters + digits, k=p_n))

    @staticmethod
    def random_tolerance(p_value, p_tolerance):
        """
        Generate a p_value within a small p_tolerance.
        Usage::
            tmp_time.sleep(random_tolerance(1.0, 0.01))
            a = random_tolerance(4.0, 0.25)
            assert 3.0 <= a <= 5.0
            True
        """
        p_value = float(p_value)
        if p_tolerance == 0.0:
            return p_value
        return p_value + p_value * random.uniform(-p_tolerance, p_tolerance)
